﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;
public partial class Bidder_TraSelectSuperviseNextArea : System.Web.UI.Page, HZ.MBSM.BLL.TraDes
{  
    public string strSupplyGoodsId="0",strProjectid="0",_strAccountid="",_strAccountid_ps="";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (HZ.MBSM.BLL.Login.CheckAssert(this.UseAccountType))
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (_Account != null)
            {
                _strAccountid = _Account.AccountId.ToString();
            }
            if (!IsPostBack)
            {
                Db DbProject = new Db();
                string strSql = "select * from area  where areaid >530000 and areaid<540000 and areaid%100=0 ";
                System.Data.IDataReader dr = DbProject.DataReader(strSql);
                lstAreaName.DataSource = dr;
                lstAreaName.DataTextField = "areaname";
                lstAreaName.DataValueField = "areaid";
                lstAreaName.DataBind();
                dr.Close();
                dr.Dispose();
                DbProject.Close();
            }
            if ((Request.QueryString["SupplyGoodsId"] != "") && (Request.QueryString["projectid"] != ""))
            {
                strSupplyGoodsId = SysFun.IsSQL(SysFun.ToName(Request.QueryString["SupplyGoodsId"].ToString()));
                strProjectid = SysFun.IsSQL(SysFun.ToName(Request.QueryString["projectid"].ToString()));
                string strSql = "select GoodsId,ProductName,GoodsName,MedicineModel,Outlookc,Unit,Pack,Factor,BidPrice,CompanyName_TB,CompanyName_SC,AccountId_PS,CompanyName_PS,Case GoodsState When '0' Then '竞价限价' WHEN '1' Then '备案采购(中标价为企业自主报价)' WHEN '2' then '不竞不议' END AS GoodsState,UserCode_SC,UserCode_TB,UserCode_PS,GoodsName,IsNull(Unit,'')+'/'+IsNull(PackUnit,'') As Pack1,QualityLevels As QualityLevels1,CASE QualityLevels " + ConfigurationManager.AppSettings["QualityLevels"].ToString() + " END AS QualityLevels from SupplyPlan_Goods where ProjectId=" + strProjectid + " And IsUse=1 And SupplyGoodsId=" + strSupplyGoodsId;
                Db ddd = new Db();
                DataTable dataa = ddd.DataTable(strSql);
                ddd.Close();
                goodsid.Text = dataa.Rows[0]["goodsid"].ToString();
                productname.Text = dataa.Rows[0]["ProductName"].ToString();
                goodsname.Text = dataa.Rows[0]["goodsname"].ToString();
                Medicinemodel.Text = dataa.Rows[0]["MedicineModel"].ToString();
                outlookc.Text = dataa.Rows[0]["Outlookc"].ToString();
                QualityLevels.Text = dataa.Rows[0]["QualityLevels"].ToString();
                Factor.Text = dataa.Rows[0]["Factor"].ToString();
                Pack1.Text = dataa.Rows[0]["Pack1"].ToString();
                BidPrice.Text = dataa.Rows[0]["BidPrice"].ToString();
                CompanyName_TB.Text = dataa.Rows[0]["CompanyName_TB"].ToString();
                CompanyName_SC.Text = dataa.Rows[0]["CompanyName_SC"].ToString();
                CompanyName_ps.Text = dataa.Rows[0]["CompanyName_ps"].ToString();
                _strAccountid_ps = dataa.Rows[0]["AccountId_PS"].ToString();
                GoodsState.Text = dataa.Rows[0]["GoodsState"].ToString();

                dataa.Clear();
                if (Request.Form["subDel"] == "删除")
                {
                    del();
                }
                if (Request.Form["subSave"] == "保  存")
                {
                    Save();
                }
                GridViewShow();
            }
            
        }
    }
    private void GridViewShow()
    {
        int i = 0;
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "SupplyPlan_GoodsNext";                                               //要查询的表明
        dbPage.Key = "id";                                               //表的主键    
        dbPage.Where =   fctConditions();                                        //查询条件    
        dbPage.SelectField = "*";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 20;                                                    //没有显示记录条数   
        dbPage.OrderBy = "areaid,id";                  //排序字段
       
         System.Text.StringBuilder strHtml = new System.Text.StringBuilder();
        strHtml.Append("<table width='100%' border='0.5' align='left' cellpadding='2' cellspacing='1' class='table_table' id='Myta'>");
        strHtml.Append("<tr>");
        strHtml.Append("<td    width='20%'>地区</td>");
        strHtml.Append("<td   width='50%'>配送企业</td>");
        strHtml.Append("<td    width='15%'>删除</td>");
        strHtml.Append("</tr>");
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        while (dr.Read())
        {
            if (i == 0)
            {
                strHtml.Append("<tr class='table_td'>");
                i++;
            }
            else
            {
                strHtml.Append("<tr class='table_td2'>");
                i--;
            }
           strHtml.Append("<td width='20%'>" + dr["areaname"].ToString() + "</td>");
            if (dr["Accountid_ps_B1"].ToString() != "")
            {
                strHtml.Append("<td width='50%' align='center' ><input type='hidden' name='companyname_ps_B1"+dr["id"]+"' value='"+dr["accountid_ps_b1"]+"'>" + dr["companyname_ps_B1"].ToString() + "</td>");
            }
            else
            {
                strHtml.Append("<td width='50%' align='center'  Style=\"color:Red;cursor:hand\"><a href='TraSelectSuperviseNextArea.aspx>添加</a></td>");
            }
            if (dr["PS_ChangeStatus"].ToString() != "1")
            {
                strHtml.Append("<td><input  type='checkbox' name='id' value='" + SysFun.ToTrim(dr["id"]) + "'</td>");
            }
            else
            {
                strHtml.Append("<td></td>");
            }
            strHtml.Append("</tr>");
        }
        strHtml.Append("</table>");
        Label1.Text = strHtml.ToString();
        
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键
       

    }
     public string fctConditions()
    {
        string _strTemp = " SupplyGoodsId="+strSupplyGoodsId+" and Accountid_ps_M="+_strAccountid;
        if ((Request.Form["txtAreaId"] != "") && (Request.Form["txtAreaId"]!=null))
        {
            _strTemp += " and areaid=" + Request.Form["txtAreaId"].ToString();
        }
         return _strTemp;
    }
    private void Save()
    {
        string strAreaname = "", strareaid = "", straccountid_ps = "", strcompanyname_ps = "", strSql = "", strSql2 = "", strSql3="";
        bool falg = false;
        if (Request.Form["txtCompanyName_PS"].ToString() != "")
        {
            strcompanyname_ps = Request.Form["txtCompanyName_PS"].ToString();
            straccountid_ps = Request.Form["txtAccountId_PS"].ToString();

            strAreaname = lstAreaName.SelectedItem.Text;
            strareaid = lstAreaName.SelectedValue;

            strSql2 = "select count(*) as cnn from SupplyPlan_GoodsNext where SupplyGoodsId=" + strSupplyGoodsId + " and  Accountid_ps_M=" + _strAccountid + " and Accountid_ps_B1=" + straccountid_ps + " and areaid=" + strareaid;
            strSql = "select count(*) as cnn from SupplyPlan_GoodsNext where SupplyGoodsId=" + strSupplyGoodsId + " and Accountid_ps_B1<>" + _strAccountid + " and Accountid_ps_M=" + _strAccountid + " and areaid=" + strareaid;
            strSql3 = "select number_ps from area where areaid=" + strareaid;
            Db ddddd = new Db();
            DataTable dt = ddddd.DataTable(strSql);
            DataTable dt2 = ddddd.DataTable(strSql2);
            DataTable dt3 = ddddd.DataTable(strSql3);
            ddddd.Close();
            int num = Convert.ToInt32(dt3.Rows[0][0].ToString());
             if (Convert.ToInt32((dt.Rows[0][0])) > num)
            {
                Response.Write("<script>alert('在该地区的配送企业已达最多数！');</script>");
            }
            else if (Convert.ToInt32((dt2.Rows[0][0])) > 0)
            {
                Response.Write("<script>alert('在该地区的该配送企业已经添加过了！');</script>");
            }
            else
            {
                if ((Convert.ToInt32((dt.Rows[0][0])) > num-1) && (_strAccountid == straccountid_ps))
                {
                    strSql = "if exists (select supplygoodsid from supplyplan_goods where supplygoodsid=" + strSupplyGoodsId + " and accountid_ps=" + _strAccountid + ")  insert into SupplyPlan_GoodsNext values('" + strSupplyGoodsId + "','" + _strAccountid + "','" + CompanyName_ps.Text + "','" + straccountid_ps + "','" + strcompanyname_ps + "','" + strareaid + "','" + strAreaname + "',0,null,'','','') ";
                    falg = true;
                }
                else if ((Convert.ToInt32((dt.Rows[0][0])) > num-1) && (_strAccountid != straccountid_ps))
                {
                    Response.Write("<script>alert('在该地区的配送企业已达最多数！');</script>");
                }
                else
                {
                    strSql = "if exists (select supplygoodsid from supplyplan_goods where supplygoodsid=" + strSupplyGoodsId + " and accountid_ps=" + _strAccountid + ")  insert into SupplyPlan_GoodsNext values('" + strSupplyGoodsId + "','" + _strAccountid + "','" + CompanyName_ps.Text + "','" + straccountid_ps + "','" + strcompanyname_ps + "','" + strareaid + "','" + strAreaname + "',0,null,'','','') ";
                    falg = true;
                }
                if (falg)
                {
                    Db dbb = new Db();
                    int kk=dbb.Command(strSql);
                                     
                    dbb.Close();

                    if (kk > 0)
                    {
                        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                        HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "插入", "SupplyPlan_GoodsNext", strSql, "../Bidder/TraSelectSuperviseNextArea.aspx");
                        Response.Write("<script>alert('添加成功！');</script>");
                    }
                    else
                    {
                        Response.Write("<script>alert('添加失败,请检查您的身份是否是此药品的主配送人！');</script>");
                    }
                    GridViewShow();
                }
            }
            dt.Clear();
            dt2.Clear();
        }
        else
        {
            Response.Write("<script>alert('配送企业不允许为空！请选择配送企业');</script>");
        }
    }
    private void del()
    {
        string strSql = "", strId;
        string[] Id ={""};
        if (Request.Form["id"] != "")
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            strId = Request.Form["id"].ToString();
            Id = strId.Split(',');
            for (int i = 0; i < Id.Length; i++)
            {
                strSql += "@" + "delete from SupplyPlan_GoodsNext where Id in (" + Id[i].ToString() + ")  and PS_ChangeStatus<>1 ";
            }
            strSql += "@" + "update HospitalPurchase set accountid_ps=null,planpurnum=null where SupplyGoodsId=" + strSupplyGoodsId + "  and Projectid=" + strProjectid;
            Db db = new Db();
            if (strSql.Length > 0)
            {
                db.ExecuteTransaction(strSql.Substring(1).Split(new char[] { '@' }), 30);
            }
            db.Close();
     
            HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "删除", "SupplyPlan_GoodsNext", strSql, "../Bidder/TraSelectSuperviseNextArea.aspx");
        }

    }
    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
   
    #region TraDes 成员

    public string Name
    {
        get { return "维护配送企业"; }
    }

    public string Describe
    {
        get { return ""; }
    }

    public string ParentName
    {
        get { return "维护配送企业"; }
    }

    public System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> UseAccountType
    {
        get
        {
            System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCompany);
            return _AccountType;
        }
    }

    #endregion

   
}
